package org.rency.crawler.common.helper;

import org.rency.common.utils.domain.OperationEnvironment;
import org.slf4j.MDC;

/**
 * 描述: MDC变量
 * 最近修改时间:2017/8/30 16:17<br>
 *
 * @author CaiyuRen
 * @version V1.0
 * @since 2017/8/30 16:17
 */
public final class MDCHelper {

  /**
   * 请求凭证
   */
  private static final String FETCH_NO = "FN";
  private static final String FETCH_URI = "FU";
  private static final String ENV_ADDR = "ADDR";

  private MDCHelper() {
  }

  /**
   * 设置MDC变量
   */
  public static void put(String fetchNo, String fetchUri) {
    MDCHelper.put(fetchNo, fetchUri, null);
  }

  /**
   * 设置MDC变量
   */
  public static void put(String fetchNo, String fetchUri, OperationEnvironment env) {
    MDC.put(FETCH_NO, fetchNo);
    MDC.put(FETCH_URI, fetchUri);
    if (env != null) {
      MDC.put(ENV_ADDR, env.getClientIp());
    }
  }

  /**
   * 移除MDC变量
   */
  public static void clear() {
    MDC.remove(FETCH_NO);
    MDC.remove(FETCH_URI);
    MDC.remove(ENV_ADDR);
  }
}
